package cn.guetzjb.sys.controller;

import cn.guetzjb.core.entity.R;
import cn.guetzjb.core.exception.ServiceException;
import cn.guetzjb.security.anotation.InnerAuth;
import cn.guetzjb.security.anotation.RequiresPermissions;
import cn.guetzjb.sys.domain.SysOperLog;
import cn.guetzjb.sys.service.SysOperLogService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/log")
@Tag(name = "日志记录")
@RequiredArgsConstructor
public class LogController {

    private final SysOperLogService logService;

    @GetMapping("/list")
    @RequiresPermissions(value = {"log:query"})
    @Operation(summary = "查询全部")
    public R all() {
        return R.ok(logService.list());
    }

    @GetMapping("/page")
    @RequiresPermissions(value = {"log:query"})
    @Operation(summary = "查询全部(分页)")
    public R page(@RequestParam(required = false, defaultValue = "1") Integer current,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                  @RequestParam(required = false) String params) {
        Page<SysOperLog> page = logService.page(new Page<>(current, pageSize), Wrappers.<SysOperLog>lambdaQuery()
                .orderByDesc(SysOperLog::getOperTime)
                .like(!StringUtils.isEmpty(params),SysOperLog::getTitle,params)
                .or()
                .like(!StringUtils.isEmpty(params),SysOperLog::getOperName,params)
                .or()
                .like(!StringUtils.isEmpty(params),SysOperLog::getBusinessType,params)
                .or()
                .like(!StringUtils.isEmpty(params),SysOperLog::getRequestMethod,params)
        );
        return R.ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions(value = {"log:query"})
    @Operation(summary = "查询(id)")
    public R getOne(@PathVariable Integer id) {
        return R.ok(logService.getById(id));
    }

    @PostMapping("/update")
    @RequiresPermissions(value = {"log:update"})
    @Operation(summary = "修改")
    public R update(@RequestBody @Valid SysOperLog sysOperLog) {
        boolean b = logService.updateById(sysOperLog);
        if (b) {
            return R.ok();
        } else {
            return R.failed("修改失败");
        }
    }

    @PostMapping("/delete/{ids}")
    @RequiresPermissions(value = {"log:delete"})
    @Operation(summary = "删除")
    public R delete(@PathVariable List<Integer> ids) {
        boolean b = logService.removeBatchByIds(ids);
        if (b) {
            return R.ok();
        } else {
            return R.failed("删除失败");
        }
    }

    @PostMapping("/add")
    @Operation(summary = "新增(inner)")
    @InnerAuth
    public R add(@RequestBody SysOperLog sysOperLog) {
        boolean b = logService.save(sysOperLog);
        if (b) {
            return R.ok();
        } else {
            throw new ServiceException("新增失败");
        }
    }

}
